package com.loveyukari.service;

import java.io.IOException;
import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
*
*yangshaohua
*2017年2月9日上午11:42:54
**/
@Service
public class YukariLogService {
	private Logger log = LoggerFactory.getLogger(YukariLogService.class);
	@Scheduled(cron = "0 0 1 * * *")
	public void moveOldLog(){
		try {
			log.info("move old log");
			String cmd = "mv /var/log/yukari/*.log.* /var/log/yukari/old/";
			Process process = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", cmd });
			if (process.waitFor() != 0){
				log.error(getInputStream(process.getErrorStream()));
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
	
	
	public String getInputStream(InputStream inputStream) throws IOException{
		byte[] bs = new byte[1024];
		StringBuilder sb = new StringBuilder();
		while (true) {
			int readLength = inputStream.read(bs, 0, bs.length);
			if (readLength == -1) {
				break;
			}
			sb.append(new String(bs, 0, readLength));
		}
		return sb.toString();
	}
	
}
